﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// IVR通道操作类
    /// 
    /// 修改纪录
    ///     2012-09-25 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class IVRChannelInfoHelper {
        /// <summary>
        /// IVR通道缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// IVR通道添加记录
        /// </summary>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>返回添加成功后的ID</returns>
        public static int Insert(IVRChannelInfo iVRChannelInfo, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(IVRChannelInfo._)
                .Value(IVRChannelInfo._ChannelID, iVRChannelInfo.ChannelID)
                .Value(IVRChannelInfo._ChannelName, iVRChannelInfo.ChannelName)
                .Value(IVRChannelInfo._CstCode, iVRChannelInfo.CstCode)
                .Value(IVRChannelInfo._LongPhone, iVRChannelInfo.LongPhone)
                .Value(IVRChannelInfo._Price, iVRChannelInfo.Price)
                .Value(IVRChannelInfo._SPCode, iVRChannelInfo.SPCode)
                .Value(IVRChannelInfo._BeginTime, iVRChannelInfo.BeginTime)
                .Value(IVRChannelInfo._EndTime, iVRChannelInfo.EndTime)
                .Value(IVRChannelInfo._UpTimeBySingle, iVRChannelInfo.UpTimeBySingle)
                .Value(IVRChannelInfo._DownTimeBySingle, iVRChannelInfo.DownTimeBySingle)
                .Value(IVRChannelInfo._HangUpRate, iVRChannelInfo.HangUpRate)
                .Value(IVRChannelInfo._NumByMonth, iVRChannelInfo.NumByMonth)
                .Value(IVRChannelInfo._TimeByMonth, iVRChannelInfo.TimeByMonth)
                .Value(IVRChannelInfo._ValidateValue, iVRChannelInfo.ValidateValue)
                .Value(IVRChannelInfo._ValidateTime, iVRChannelInfo.ValidateTime)
                .Value(IVRChannelInfo._UserDayMOMoney, iVRChannelInfo.UserDayMOMoney)
                .Value(IVRChannelInfo._UserMonthMOMoney, iVRChannelInfo.UserMonthMOMoney)
                .Value(IVRChannelInfo._UserMonthMRMoney, iVRChannelInfo.UserMonthMRMoney)
                .Value(IVRChannelInfo._DayMOMoney, iVRChannelInfo.DayMOMoney)
                .Value(IVRChannelInfo._DayMRMoney, iVRChannelInfo.DayMRMoney)
                .Value(IVRChannelInfo._Chinese, iVRChannelInfo.Chinese)
                .Value(IVRChannelInfo._English, iVRChannelInfo.English)
                .Value(IVRChannelInfo._Phone, iVRChannelInfo.Phone)
                .Value(IVRChannelInfo._UsePrivence, iVRChannelInfo.UsePrivence)
                .Value(IVRChannelInfo._Status, iVRChannelInfo.Status)
                .Value(IVRChannelInfo._HangUpTime, iVRChannelInfo.HangUpTime)
                .Value(IVRChannelInfo._HangDownTime, iVRChannelInfo.HangDownTime)
                .Value(IVRChannelInfo._CreateTime, iVRChannelInfo.CreateTime)
                .Value(IVRChannelInfo._Settlement, iVRChannelInfo.Settlement)
                .Value(IVRChannelInfo._Remark, iVRChannelInfo.Remark)
                .ToString();
            strSql += ";select SCOPE_IDENTITY();";
            object objValue = dbkey.IsNullEmpty() ? Data.GetScalar(strSql) : Data.Pool(dbkey).GetScalar(strSql);
            if (objValue.IsAllNull()) return 0;
            int value = objValue.ToInt();
            if (delCache.IsNull()) return value;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.IVRChannelInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.IVRChannelInfoCache_" + s + ")" : s);
            }
            return value;
        }
        /// <summary>
        /// IVR通道添加记录
        /// </summary>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <returns>返回添加成功后的ID</returns>
        public static int Insert(IVRChannelInfo iVRChannelInfo, string dbkey = "") {
            return Insert(iVRChannelInfo, dbkey, null);
        }
        /// <summary>
        /// IVR通道修改记录
        /// </summary>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(IVRChannelInfo iVRChannelInfo, string dbkey = "", string where = "", string[] delCache = null) {
            if (iVRChannelInfo.ChannelID.IsNull()) return false;
            string strSql = new SQL().Update(IVRChannelInfo._)
                .Set(IVRChannelInfo._ChannelName, iVRChannelInfo.ChannelName)
                .Set(IVRChannelInfo._CstCode, iVRChannelInfo.CstCode)
                .Set(IVRChannelInfo._LongPhone, iVRChannelInfo.LongPhone)
                .Set(IVRChannelInfo._Price, iVRChannelInfo.Price)
                .Set(IVRChannelInfo._SPCode, iVRChannelInfo.SPCode)
                .Set(IVRChannelInfo._BeginTime, iVRChannelInfo.BeginTime)
                .Set(IVRChannelInfo._EndTime, iVRChannelInfo.EndTime)
                .Set(IVRChannelInfo._UpTimeBySingle, iVRChannelInfo.UpTimeBySingle)
                .Set(IVRChannelInfo._DownTimeBySingle, iVRChannelInfo.DownTimeBySingle)
                .Set(IVRChannelInfo._HangUpRate, iVRChannelInfo.HangUpRate)
                .Set(IVRChannelInfo._NumByMonth, iVRChannelInfo.NumByMonth)
                .Set(IVRChannelInfo._TimeByMonth, iVRChannelInfo.TimeByMonth)
                .Set(IVRChannelInfo._ValidateValue, iVRChannelInfo.ValidateValue)
                .Set(IVRChannelInfo._ValidateTime, iVRChannelInfo.ValidateTime)
                .Set(IVRChannelInfo._UserDayMOMoney, iVRChannelInfo.UserDayMOMoney)
                .Set(IVRChannelInfo._UserMonthMOMoney, iVRChannelInfo.UserMonthMOMoney)
                .Set(IVRChannelInfo._UserMonthMRMoney, iVRChannelInfo.UserMonthMRMoney)
                .Set(IVRChannelInfo._DayMOMoney, iVRChannelInfo.DayMOMoney)
                .Set(IVRChannelInfo._DayMRMoney, iVRChannelInfo.DayMRMoney)
                .Set(IVRChannelInfo._Chinese, iVRChannelInfo.Chinese)
                .Set(IVRChannelInfo._English, iVRChannelInfo.English)
                .Set(IVRChannelInfo._Phone, iVRChannelInfo.Phone)
                .Set(IVRChannelInfo._UsePrivence, iVRChannelInfo.UsePrivence)
                .Set(IVRChannelInfo._Status, iVRChannelInfo.Status)
                .Set(IVRChannelInfo._HangUpTime, iVRChannelInfo.HangUpTime)
                .Set(IVRChannelInfo._HangDownTime, iVRChannelInfo.HangDownTime)
                .Set(IVRChannelInfo._CreateTime, iVRChannelInfo.CreateTime)
                .Set(IVRChannelInfo._Settlement, iVRChannelInfo.Settlement)
                .Set(IVRChannelInfo._Remark, iVRChannelInfo.Remark)
                .Where("1=1").Where(new Where()
                    .And(IVRChannelInfo._ChannelID, iVRChannelInfo.ChannelID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.IVRChannelInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.IVRChannelInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// IVR通道修改记录
        /// </summary>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(IVRChannelInfo iVRChannelInfo, string dbkey = "") {
            return Update(iVRChannelInfo, dbkey, "", null);
        }
        /// <summary>
        /// IVR通道修改多条记录
        /// </summary>
        /// <param name="channelIDList">IVR通道编号列表，用“,”号分隔</param>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string channelIDList,  IVRChannelInfo iVRChannelInfo, string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Update(IVRChannelInfo._)
                .Set(IVRChannelInfo._ChannelName, iVRChannelInfo.ChannelName)
                .Set(IVRChannelInfo._CstCode, iVRChannelInfo.CstCode)
                .Set(IVRChannelInfo._LongPhone, iVRChannelInfo.LongPhone)
                .Set(IVRChannelInfo._Price, iVRChannelInfo.Price)
                .Set(IVRChannelInfo._SPCode, iVRChannelInfo.SPCode)
                .Set(IVRChannelInfo._BeginTime, iVRChannelInfo.BeginTime)
                .Set(IVRChannelInfo._EndTime, iVRChannelInfo.EndTime)
                .Set(IVRChannelInfo._UpTimeBySingle, iVRChannelInfo.UpTimeBySingle)
                .Set(IVRChannelInfo._DownTimeBySingle, iVRChannelInfo.DownTimeBySingle)
                .Set(IVRChannelInfo._HangUpRate, iVRChannelInfo.HangUpRate)
                .Set(IVRChannelInfo._NumByMonth, iVRChannelInfo.NumByMonth)
                .Set(IVRChannelInfo._TimeByMonth, iVRChannelInfo.TimeByMonth)
                .Set(IVRChannelInfo._ValidateValue, iVRChannelInfo.ValidateValue)
                .Set(IVRChannelInfo._ValidateTime, iVRChannelInfo.ValidateTime)
                .Set(IVRChannelInfo._UserDayMOMoney, iVRChannelInfo.UserDayMOMoney)
                .Set(IVRChannelInfo._UserMonthMOMoney, iVRChannelInfo.UserMonthMOMoney)
                .Set(IVRChannelInfo._UserMonthMRMoney, iVRChannelInfo.UserMonthMRMoney)
                .Set(IVRChannelInfo._DayMOMoney, iVRChannelInfo.DayMOMoney)
                .Set(IVRChannelInfo._DayMRMoney, iVRChannelInfo.DayMRMoney)
                .Set(IVRChannelInfo._Chinese, iVRChannelInfo.Chinese)
                .Set(IVRChannelInfo._English, iVRChannelInfo.English)
                .Set(IVRChannelInfo._Phone, iVRChannelInfo.Phone)
                .Set(IVRChannelInfo._UsePrivence, iVRChannelInfo.UsePrivence)
                .Set(IVRChannelInfo._Status, iVRChannelInfo.Status)
                .Set(IVRChannelInfo._HangUpTime, iVRChannelInfo.HangUpTime)
                .Set(IVRChannelInfo._HangDownTime, iVRChannelInfo.HangDownTime)
                .Set(IVRChannelInfo._CreateTime, iVRChannelInfo.CreateTime)
                .Set(IVRChannelInfo._Settlement, iVRChannelInfo.Settlement)
                .Set(IVRChannelInfo._Remark, iVRChannelInfo.Remark)
                .Where("1=1").Where(new Where()
                    .And(IVRChannelInfo._ChannelID, "(" + channelIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.IVRChannelInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.IVRChannelInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// IVR通道修改多条记录
        /// </summary>
        /// <param name="channelIDList">IVR通道编号列表，用“,”号分隔</param>
        /// <param name="iVRChannelInfo">IVR通道实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string channelIDList,  IVRChannelInfo iVRChannelInfo, string dbkey = "") {
            return UpdateByIDList(channelIDList,  iVRChannelInfo, dbkey, "", null);
        }
         /// <summary>
        /// IVR通道删除记录
        /// </summary>
        /// <param name="channelID">IVR通道编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? channelID,  string dbkey = "", string where = "", string[] delCache = null) {
            if (channelID.IsNull()) return false;
            string strSql = new SQL().Delete(IVRChannelInfo._)
                .Where("1=1").Where(new Where()
                    .And(IVRChannelInfo._ChannelID, channelID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.IVRChannelInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.IVRChannelInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// IVR通道删除记录
        /// </summary>
        /// <param name="channelID">IVR通道编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? channelID, string dbkey = "") {
            return DeleteByID(channelID,  dbkey, "", null);
        }
        /// <summary>
        /// IVR通道删除记录
        /// </summary>
        /// <param name="channelID">IVR通道编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int channelID,  string dbkey = "", string where = "", string[] delCache = null) {
            return DeleteByID((int?)channelID,  dbkey, where, delCache);
        }
        /// <summary>
        /// IVR通道删除记录
        /// </summary>
        /// <param name="channelID">IVR通道编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int channelID, string dbkey = "") {
            return DeleteByID((int?)channelID,  dbkey, "", null);
        }
        /// <summary>
        /// IVR通道删除多条记录
        /// </summary>
        /// <param name="channelIDList">IVR通道编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string channelIDList,  string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Delete(IVRChannelInfo._)
                .Where("1=1").Where(new Where()
                    .And(IVRChannelInfo._ChannelID, "(" + channelIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.IVRChannelInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.IVRChannelInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// IVR通道删除多条记录
        /// </summary>
        /// <param name="channelIDList">IVR通道编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string channelIDList, string dbkey = "") {
            return DeleteByIDList(channelIDList,  dbkey, "", null);
        }
        /// <summary>
        /// IVR通道查询所有记录
        /// </summary>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回实体记录集</returns>
        public static IList<IVRChannelInfo> SelectListByAll(string dbkey = "", string where = "", string order = "", string fieldList = "") {
            string cacheNameKey = "GTS.MiniBox.IVRChannelInfoCache_SelectListByAll_{0}_{1}_{2}".FormatWith(where, order, fieldList);
            return Cache2.Get<IList<IVRChannelInfo>>(cacheNameKey, cacheSeconds, () => {
                string strSql = string.Empty;
			    if (fieldList.IsNullEmpty()) {
                    strSql = new SQL().From(IVRChannelInfo._)
                        .Select(IVRChannelInfo._ChannelID)
                        .Select(IVRChannelInfo._ChannelName)
                        .Select(IVRChannelInfo._CstCode)
                        .Select(IVRChannelInfo._LongPhone)
                        .Select(IVRChannelInfo._Price)
                        .Select(IVRChannelInfo._SPCode)
                        .Select(IVRChannelInfo._BeginTime)
                        .Select(IVRChannelInfo._EndTime)
                        .Select(IVRChannelInfo._UpTimeBySingle)
                        .Select(IVRChannelInfo._DownTimeBySingle)
                        .Select(IVRChannelInfo._HangUpRate)
                        .Select(IVRChannelInfo._NumByMonth)
                        .Select(IVRChannelInfo._TimeByMonth)
                        .Select(IVRChannelInfo._ValidateValue)
                        .Select(IVRChannelInfo._ValidateTime)
                        .Select(IVRChannelInfo._UserDayMOMoney)
                        .Select(IVRChannelInfo._UserMonthMOMoney)
                        .Select(IVRChannelInfo._UserMonthMRMoney)
                        .Select(IVRChannelInfo._DayMOMoney)
                        .Select(IVRChannelInfo._DayMRMoney)
                        .Select(IVRChannelInfo._Chinese)
                        .Select(IVRChannelInfo._English)
                        .Select(IVRChannelInfo._Phone)
                        .Select(IVRChannelInfo._UsePrivence)
                        .Select(IVRChannelInfo._Status)
                        .Select(IVRChannelInfo._HangUpTime)
                        .Select(IVRChannelInfo._HangDownTime)
                        .Select(IVRChannelInfo._CreateTime)
                        .Select(IVRChannelInfo._Settlement)
                        .Select(IVRChannelInfo._Remark)
                        .ToString();
                } else {
			        strSql = new SQL().From(IVRChannelInfo._).Select(fieldList).ToString();
                }
                if (!where.IsNullEmpty()) strSql += " where 1=1 " + where;
                if (!order.IsNullEmpty()) strSql += " order by " + order;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<IVRChannelInfo>();
            });
        }
        /// <summary>
        /// IVR通道查询所有记录
        /// </summary>
        /// <returns>返回实体记录集</returns>
        public static IList<IVRChannelInfo> SelectListByAll(string dbkey = "") {
            return SelectListByAll(dbkey, "", "", "");
        }
        /// <summary>
        /// 清除IVR通道查询所有记录的缓存
        /// </summary>
        public static void ClearCacheSelectListByAll() {
            //Cache2.Remove("GTS.MiniBox.IVRChannelInfoCache_SelectListByAll___");
            Cache2.RemoveByPattern("GTS.MiniBox.IVRChannelInfoCache_SelectListByAll_(.+?)");
        }
        /// <summary>
        /// 清除IVR通道所有缓存
        /// </summary>
        public static void ClearCacheAll() {
            Cache2.RemoveByPattern("GTS.MiniBox.IVRChannelInfoCache_(.+?)");
        }
    }
}

